﻿#region License

// // -------------------------------------------------------------------------------------
// // This information refers to RabbitMQBase.cs
// // 
// // Copyright 2012 ELEN7046 Group 2
// // 
// // Last Modified on: 2012-06-10
// // 
// // Contributors:
// // 
// // Amith Sewnarain
// // Elmar Vosloo
// // Jason Thorne
// // Satish Jewan
// // 
// // Wits University 
// // School of Electrical & Information Engineering
// // Software Engineering Tools and Techniques
// // 
// // -------------------------------------------------------------------------------------
// // 
// // This file (RabbitMQBase.cs) is part of the DishDash Event Visualization System.
// // 
// // DishDash is free software: you can redistribute it and/or modify
// // it under the terms of the GNU General Public License as published by
// // the Free Software Foundation, either version 3 of the License, or
// // (at your option) any later version.
// // 
// // DishDash is distributed in the hope that it will be useful,
// // but WITHOUT ANY WARRANTY; without even the implied warranty of
// // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// // GNU General Public License for more details.
// // 
// // You should have received a copy of the GNU General Public License
// // along with DishDash.  
// // If not, see <http://www.gnu.org/licenses/>.
// // -------------------------------------------------------------------------------------

#endregion

#region Using Directives

using System;
using RabbitMQ.Client;
using RabbitMQ.Client.Exceptions;

#endregion

namespace RabbitMQIntegration
{
    public class RabbitMQBase
    {
        protected static bool IsConsuming;
        protected static bool IsDurable;
        protected static bool AutoAck;
        protected static string QueueName;

        private static RabbitMQBase _instance = null;
        private static readonly object _syncObject = new object();

        protected RabbitMQBase()
        {
            if (System.Configuration.ConfigurationSettings.AppSettings != null)
            {
                Server = System.Configuration.ConfigurationSettings.AppSettings["Server"];
                ExchangeName = System.Configuration.ConfigurationSettings.AppSettings["ExchangeName"];
                ExchangeTypeName = System.Configuration.ConfigurationSettings.AppSettings["ExchangeType"];
            }
            IsDurable = true;
            AutoAck = true;

            try
            {
                var connectionFactory = new ConnectionFactory();
                connectionFactory.HostName = Server;
                Connection = connectionFactory.CreateConnection();
                Model = Connection.CreateModel();
                Model.ConfirmSelect();
                if (!String.IsNullOrEmpty(ExchangeName))
                {
                    Model.ExchangeDeclare(ExchangeName, ExchangeTypeName, IsDurable);
                }
            }
            catch (BrokerUnreachableException)
            {
                throw;
            }
        }

        public static string Server { get; set; }
        public static string ExchangeName { get; set; }
        public static string ExchangeTypeName { get; set; }
        public static string RoutingKey { get; set; }
        public IModel Model { get; set; }
        public IConnection Connection { get; set; }

        /// <summary>
        ///   Gets the instance.
        /// </summary>
        public static RabbitMQBase Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_syncObject)
                    {
                        if (_instance == null)
                        {
                            _instance = new RabbitMQBase();
                        }
                    }
                }
                return _instance;
            }
        }
    }
}